//
//  ChangeUserNameViewController.m
//  huashiedu
//
//  Created by oceans on 2018/9/1.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ChangeUserNameViewController.h"
#import "UserModel.h"
#import "TipManager.h"

@interface ChangeUserNameViewController () <UITextFieldDelegate>

@property (strong, nonatomic) UITextField *textfield;
@end

@implementation ChangeUserNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
}

- (void)setupView {
    
    self.view.backgroundColor = COLOR(245, 245, 245, 1);
    
    if (_type == 0) {
        self.navigationItem.title = @"昵称";
    }else {
        self.navigationItem.title = @"姓名";
    }
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(editFinished)];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1.0], NSFontAttributeName: [UIFont systemFontOfSize:15.0]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = item;
    
    self.textfield = [[UITextField alloc] initWithFrame:CGRectMake(0, XNavigationHeight + 20, ScreenWidth, 40)];
    _textfield.backgroundColor = [UIColor whiteColor];
    _textfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textfield.delegate = self;
    
    UIView *right = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
    _textfield.leftView = right;
    _textfield.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:_textfield];
}

- (void)editFinished {
    
    if (!(_textfield.text.length == 0 || [_textfield.text isEqualToString:[UserModel shared].loginUser.nickname])) {
        
        [self.view endEditing:YES];
        __weak typeof(self) weakself = self;
        __block NSString *name = _textfield.text;
        if (self.type == 0) {
            
            [[UserModel shared] updateUserinfo:@"nickname" value:_textfield.text successBlock:^{
                [TipManager showTip:@"修改成功"];
                [weakself.navigationController popViewControllerAnimated:YES];
            }];
        }else {
            [[UserModel shared] updateUserinfo:@"realname" value:_textfield.text successBlock:^{
                [TipManager showTip:@"修改成功"];
                [weakself.navigationController popViewControllerAnimated:YES];
            }];
        }
        
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self editFinished];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
